package com.apexedu.sync.engine.impl;

import com.apexedu.biz.entity.TAccessAppReg;
import com.apexedu.framework.dao.BaseDao;
import com.apexedu.identity.entity.TSysMenu;
import com.apexedu.identity.entity.TSysRole;
import com.apexedu.identity.entity.TSysUser;
import com.apexedu.portal.util.DBMUtil;
import com.apexedu.sync.contant.Constants;
import com.apexedu.sync.engine.ISyncContext;
import com.apexedu.sync.engine.ISyncEngine;
import com.apexedu.sync.entity.SyncAppMsg;
import com.apexedu.sync.exception.AuCSyncException;

import java.util.HashMap;
import java.util.Map;

/**
 * 接入系统同步上下文
 * Created by ly on 2015/1/12.
 */
public class SyncContext implements ISyncContext {

    /**
     * 接入系统
     */
    private TAccessAppReg accessAppReg;
    /**
     * hibernate数据库操作类
     */
    private BaseDao baseDao;
    /**
     * 数据库操作类
     */
    private DBMUtil dbmUtil;
    /**
     * 操作人
     */
    private TSysUser user;
    /**
     * 操作引擎
     */
    private ISyncEngine engine;
    /**
     * 同步结果
     */
    private SyncAppMsg msg;
    /**
     * 角色集合 <bizcode,角色>
     */
    private Map<String, TSysRole> mapRole = new HashMap<String, TSysRole>();
    /**
     * 菜单资源集合 <bizcode,菜单资源>
     */
    private Map<String, TSysMenu> mapMenu = new HashMap<String, TSysMenu>();

    /**
     * 构建同步上下文
     *
     * @param accessAppReg
     * @param user
     * @return
     */
    public static ISyncContext build(ISyncEngine engine, BaseDao baseDao, TAccessAppReg accessAppReg, TSysUser user) throws AuCSyncException {
        SyncContext context = new SyncContext();
        context.engine = engine;
        context.baseDao = baseDao;
        context.accessAppReg = accessAppReg;
        context.user = user;
        context.dbmUtil = new DBMUtil(accessAppReg.getDb());
        context.msg = new SyncAppMsg();
        return context;
    }

    @Override
    public boolean isLivebos() {
        return Constants.TrueOrFalse.TRUE.equals(this.accessAppReg.getXtlx());
    }

    @Override
    public boolean isApexssh() {
        return Constants.TrueOrFalse.FALSE.equals(this.accessAppReg.getXtlx());
    }

    @Override
    public String getAppid() {
        return this.accessAppReg.getAppid();
    }

    @Override
    public TAccessAppReg getAccessAppReg() {
        return accessAppReg;
    }

    @Override
    public DBMUtil getDbmUtil() {
        return dbmUtil;
    }

    @Override
    public BaseDao getBaseDao() {
        return this.baseDao;
    }

    @Override
    public TSysUser getUser() {
        return user;
    }

    @Override
    public SyncAppMsg getMsg() {
        return msg;
    }

    @Override
    public Map<String, TSysRole> getMapRole() {
        return mapRole;
    }

    @Override
    public Map<String, TSysMenu> getMapMenu() {
        return mapMenu;
    }

    @Override
    public Map<String, TSysUser> getMapUser() {
        return this.engine.getMapUser();
    }
}
